package com.creditharmony.car.loan.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.creditharmony.car.common.base.service.impl.BaseServiceImpl;
import com.creditharmony.car.common.common.vo.requestvo.CarLoanInfoRequestVo;
import com.creditharmony.car.common.common.vo.responsevo.CarLoanInfoResponseVo;
import com.creditharmony.car.common.exception.UserException;
import com.creditharmony.car.common.exception.UserExceptionConstants;
import com.creditharmony.car.common.utils.ErrorType;
import com.creditharmony.car.common.utils.UuidUtil;
import com.creditharmony.car.loan.dao.CarLoanInfoDao;
import com.creditharmony.car.loan.entity.CarLoanInfoEntity;
import com.creditharmony.car.loan.service.CarLoanInfoService;


/**
 * 借款Service
 * @Class Name CarLoanInfoServiceImpl
 * @author zqa
 * @Create In 2018年1月24日
 */
@Service
public class CarLoanInfoServiceImpl extends BaseServiceImpl<CarLoanInfoEntity> implements CarLoanInfoService {
    
    private static final Logger logger = LoggerFactory.getLogger(CarLoanInfoServiceImpl.class);
            
    @Autowired
    private CarLoanInfoDao carLoanInfoDao;

    @Override
    public void saveLoanInfo(CarLoanInfoRequestVo vo) {
        CarLoanInfoEntity info = new CarLoanInfoEntity();
        try {
            BeanUtils.copyProperties(vo, info);
        } catch (BeansException e) {
            logger.error("CarLoanInfoEntity对象拷贝属性值抛出异常",e);
            throw new UserException(UserExceptionConstants.BEAN_COPY_ERROR,e);
        }
        try {
            info.setId(UuidUtil.getUUID());
            carLoanInfoDao.insertSelective(info);
        } catch (Exception e) {
            logger.error(UserExceptionConstants.DB_SYSTEM_ERROR, e);
            throw new UserException(UserExceptionConstants.DB_SYSTEM_ERROR, e);
        }
    }

    @Override
    public CarLoanInfoResponseVo queryCarLoanInfoByLoanCode(String loanCode) {
        CarLoanInfoEntity entity;
        try {
            entity = carLoanInfoDao.queryCarLoanInfoByLoanCode(loanCode);
        } catch (Exception e) {
            logger.error(UserExceptionConstants.DB_SYSTEM_ERROR, e);
            throw new UserException(UserExceptionConstants.DB_SYSTEM_ERROR, e);
        }
        CarLoanInfoResponseVo resVo = new CarLoanInfoResponseVo();
        try {
            BeanUtils.copyProperties(entity, resVo);
        } catch (BeansException e) {
            logger.error("CarLoanInfoEntity对象拷贝属性值抛出异常",e);
            throw new UserException(UserExceptionConstants.BEAN_COPY_ERROR,e);
        }
        return resVo;
    }

    @Override
    public void updateLoanInfo(CarLoanInfoRequestVo vo) {
        CarLoanInfoEntity entity = new CarLoanInfoEntity();
        try {
            BeanUtils.copyProperties(vo, entity);
            carLoanInfoDao.updateLoanInfo(entity);
        } catch (BeansException e) {
            logger.error("CarLoanInfoEntity对象拷贝属性值抛出异常",e);
            throw new UserException(UserExceptionConstants.BEAN_COPY_ERROR,e);
        }catch(Exception e){
            logger.error(UserExceptionConstants.DB_SYSTEM_ERROR, e);
            throw new UserException(UserExceptionConstants.DB_SYSTEM_ERROR, e);
        }
        
    }
}
